<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Link dialog window.
-->
<html>
<head>
        <title>Template Properties</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="robots" content="noindex, nofollow" />
	<script type="text/javascript">

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;
var FCKRegexLib	= oEditor.FCKRegexLib ;
var FCKTools	= oEditor.FCKTools ;

document.write( '<script src="' + FCKConfig.BasePath + 'dialog/common/fck_dialog_common.js" type="text/javascript"><\/script>' ) ;

	</script>
	<script type="text/javascript">

//#### Dialog Tabs

// Set the dialog tabs.
window.parent.AddTab( 'Info', FCKLang.wikiDlgTemplateTabInfo || 'Info' ) ;
window.parent.AddTab( 'Form', FCKLang.wikiDlgTemplateTabForm || 'Form' ) ;
window.parent.AddTab( 'Source', FCKLang.wikiDlgTemplateTabSource || 'Source' ) ;

function OnDialogTabChange( tabCode )
{
	ShowE('divInfo'		, ( tabCode == 'Info' ) ) ;
	ShowE('divForm' 	, ( tabCode == 'Form' ) ) ;
        ShowE('divSource' 	, ( tabCode == 'Source' ) ) ;
}

// Get the selected flash embed (if available).
var oFakeImage = FCK.Selection.GetSelectedElement() ;
var oTemplateSpan ;
var sTemplateName ;

if ( oFakeImage )
{
	if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fck_mw_template') )
		oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
	else
		oFakeImage = null ;
}

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;

	// Load the selected link information (if any).
	LoadSelection() ;

        // Load template list
        LoadTemplateList();

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
	window.parent.SetAutoSize( true ) ;

        // Set image paths
        SetPendingLoaderImagePath();
}

function SetPendingLoaderImagePath() {
    var src = FCKConfig.PluginsPath + 'mediawiki/images/ajax-loader.gif';
    GetE('xTemplateManualPending').getElementsByTagName('img')[0].src = src;
    GetE('xTemplateFormPending').getElementsByTagName('img')[0].src = src;
}

function LoadSelection()
{
	if ( !oTemplateSpan ) return ;

	var inputText = FCKTools.HTMLDecode(oTemplateSpan.innerHTML);
	if (inputText.length>0 && inputText.indexOf('{{#')<0 && inputText.indexOf('{{:')<0 ) 
	{
                // check where the first | appears
                var pipe = inputText.indexOf('|');
                // check where the first linebreak appears
                var lr = inputText.indexOf('fckLR');
                
                // if both chars appear, what comes earlier terminates the template name
                if (pipe > -1 && lr > -1)
                    sTemplateName = inputText.substring(2, (pipe > lr) ? lr : pipe);
                else if (pipe > -1) // | was found only
                    sTemplateName = inputText.substring(2, pipe);
                else if (lr > -1)  // linebreak was found only
                    sTemplateName = inputText.substring(2, lr);
                else // no linebreak and | found template name is up to the closing }}
                    sTemplateName = inputText.substring(2, inputText.indexOf('}}'));
                // capitalize the first letter of the name
		sTemplateName = sTemplateName.charAt(0).toUpperCase() + sTemplateName.substr(1)
                // set divInfo and divForm with data for current seleced template
		SetUrl('Template:' + sTemplateName);
	}
        // set template source code
	GetE('xTemplateRaw').value = inputText.replace(/fckLR/g,'\r\n').replace( /&quot;/g, '"' );
        // remove selection list
        var parentTd = GetE('divList').parentNode;
        var parentTr = parentTd.parentNode;
        parentTr.removeChild(parentTd);
        // show selected label on top
        GetE('divHeaderSelected').style.display = 'block';
        GetE('divHeaderSelected').innerHTML += ' ' + sTemplateName
}

function LoadFormForTemplate() {
	// Make an Ajax search for the pages.
	oEditor.window.parent.sajax_request_type = 'GET' ;
	oEditor.window.parent.sajax_do_call( 'wfSajaxFormForTemplateFCKeditor',[sTemplateName], SetFormForTemplate ) ;
}

function SetFormForTemplate(result) {
	var results = result.responseText.Trim().split( '\n' ) ;

        if ( results.length > 0 && results[0].length > 0 ) {
            //http://localhost/wiki/index.php/Special:EditDataEmbedded/Form_name/page_name?page=plain
            var url = oEditor.window.parent.wgServer
                    + oEditor.window.parent.wgScript
                    + '/Special:EditDataEmbedded/'
                    + results[0].substr(results[0].indexOf(':') + 1).replace(' ', '_')
                    + '/'
                    + oEditor.window.parent.wgPageName.replace(' ', '_')
                    + '?page=plain';
            var xTemplateForm = GetE('xTemplateForm');
            if (xTemplateForm.tagName.toLowerCase == "iframe") {
                SetAttribute(GetE('xTemplateForm'), 'src', url);
            }
            else {
                xTemplateForm.parentNode.parentNode.parentNode.parentNode.border = '1';
                xTemplateForm.parentNode.innerHTML = '<iframe id="xTemplateForm" scrolling="yes" width="100%" height="100%" src="' + url + '" onload="javascript:ShowTemplateForm();"></iframe>';
            }
	}
        else {
            // set table border thick
            ShowTemplateForm();
            GetE('xTemplateForm').parentNode.parentNode.parentNode.parentNode.border = '1';
            GetE('xTemplateForm').parentNode.innerHTML = '<div id="xTemplateForm">'
                + FCKLang.wikiDlgTemplateNoForm ||
                  'There is no form to submit parameters for this template call. Please edit the source code directly.'
                + '</div>';
        }
}

function LoadTemplateList() {
        // check if the list is required anyway, if not just quit
        if (! GetE('xTemplateList')) return;
        
	// Make an Ajax search for the pages.
	oEditor.window.parent.sajax_request_type = 'GET' ;
	oEditor.window.parent.sajax_do_call( 'wfSajaxTemplateListFCKeditor',[oEditor.window.parent.wgPageName], FillTemplateList ) ;
}
function FillTemplateList(result) {
	var results = result.responseText.Trim().split( '\n' ) ;
	var select = GetE( 'xTemplateList' ) ;

	if ( results.length > 0 && !( results.length == 1 && results[0].length == 0 ) )
	{
		for ( var i = 0 ; i < results.length ; i++ ) {
                        var shownVal = (results[i].indexOf(':') != -1)
                                    ? results[i].substr(results[i].indexOf(':') + 1)
                                    : results[i];
			FCKTools.AddSelectOption( select, shownVal, results[i] ) ;
                        // select the current entry, when there is a selecion loaded
                        // actually it's not used anymore but left here
                        // incase someone needs it in future
                        if (sTemplateName && sTemplateName == shownVal)
                            document.getElementById('xTemplateList').childNodes[i + 1].setAttribute('selected', 'selected');
                }
	}

}
function SetUrl(tpage) {
    // trigger the pending indicators visible in the Info and Form tab
    ShowPendingIndicators();
    
    sTemplateName = tpage;
    LoadFormForTemplate();
    var urlTemplate = oEditor.window.parent.wgServer
        + oEditor.window.parent.wgScript
        + '?title=' + tpage + '&page=plain';

    SetAttribute(GetE('xTemplateManual'),'src',urlTemplate);
    GetE('xTemplateRaw').value = "{{" + tpage.substr(tpage.indexOf(':') + 1) + "}}";
}

function ShowTemplateManual() {
    GetE('xTemplateManualPending').style.display = "none";
    GetE('xTemplateManualFrame').style.display = "block";
}

function ShowTemplateForm() {
    GetE('xTemplateFormPending').style.display = "none";
    GetE('xTemplateFormFrame').style.display = "block";
}

function ShowPendingIndicators() {
    // Template Info
    GetE('xTemplateManualPending').style.display = "block";
    GetE('xTemplateManualFrame').style.display = "none";
    // Form
    GetE('xTemplateFormPending').style.display = "block";
    GetE('xTemplateFormFrame').style.display = "none";
}

//#### The OK button was hit.
function Ok()
{
	if ( !oTemplateSpan )
	{
		oTemplateSpan = FCK.EditorDocument.createElement( 'SPAN' ) ;
		oTemplateSpan.className = 'fck_mw_template' ;
	}
	
	var templateData = FCKTools.HTMLEncode(GetE('xTemplateRaw').value.Trim().replace(/(\r\n|\n)/g, 'fckLR')).replace( /"/g, '&quot;' ) ;
    var tooltip = FCKTools.HTMLEncode(GetE('xTemplateRaw').value.Trim()).replace( /"/g, '&quot;' );
	
	if ( !( /^{{[\s\S]+}}$/.test( templateData ) ) )
	{
		alert( FCKLang.wikiDlgTemplateSyntaxErr || 'Templates must start with {{ and end with }}. Please check it.' ) ;
		return false ;
	}
	
	oTemplateSpan.innerHTML = templateData ;

	if ( !oFakeImage )
	{
		oFakeImage	= oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__MWTemplate', oTemplateSpan ) ;
		oFakeImage.setAttribute( '_fck_mw_template', 'true', 0 ) ;
                oFakeImage.setAttribute( 'title', tooltip);
                oFakeImage.setAttribute( 'alt', tooltip);
		oFakeImage	= FCK.InsertElement( oFakeImage ) ;
	}
        else {
                oFakeImage.setAttribute( 'title', tooltip);
                oFakeImage.setAttribute( 'alt', tooltip);
        }

	return true ;
}

	</script>
        <style type="text/css">
            .tmplTable {
                width: 100%;
                height: 100%;
                vertical-align: top;
            }
            .tmplTable table {
                border: 0;
                padding: 0;
                margin: 0;
            }
            .tmplDivOptionBox {
                height: 100%;
            }
        </style>
</head>
<body style="overflow: hidden">
    <div id="divLabelSeleced"></div>
    <table class="tmplTable">
        <tr>
            <td><div id="divHeaderSelected" style="display: none;"><b><span fcklang="wikiDlgTemplateSelected">Template selected</span>:</b></div></td>
        </tr>
        <tr class="tmplTable">
            <td style="width: 30%; vertical-align: top;">
                <div id="divList" style="height: 100%; width: 100%;">
                    <b><span fcklang="wikiDlgTemplateChoose">Choose a template</span></b>
                    <select id="xTemplateList" onchange="SetUrl( this.value );" size="10" style="height: 100%; width: 100%;">
                    </select>
                </div>
            </td>
            <td valign="top">
                <div id="divInfo" class="tmplDivOptionBox">
                    <b><span fcklang="wikiDlgTemplateDescr">Template description</span></b>
                    <table class="tmplTable">
			<tr>
				<td class="tmplTable">
                                    <div id="xTemplateManualPending" style="display:none; width: 100%; height: 100%; border: 1px solid;">
                                        <img src="" alt="loading..." />
                                    </div>
                                    <div id="xTemplateManualFrame" style="display:block; width: 100%; height: 100%;">
                                        <iframe id="xTemplateManual" width="100%" scrolling="yes" height="100%" src="" onload="javascript:ShowTemplateManual();"></iframe>
                                    </div>
				</td>
			</tr>
                    </table>
                </div>
                <div id="divForm" class="tmplDivOptionBox" style="display: none">
                    <b><span fcklang="wikiDlgTemplateFillData">Fill the template with data</span></b>
                    <table class="tmplTable">
			<tr>
				<td class="tmplTable">
                                    <div id="xTemplateFormPending" style="display:none; width: 100%; height: 100%; border: 1px solid;">
                                        <img src="" alt="loading" />
                                    </div>
                                    <div id="xTemplateFormFrame" style="display:block; width: 100%; height: 100%;">
                                        <iframe id="xTemplateForm" scrolling="yes" width="100%" height="100%" src="" onload="javascript:ShowTemplateForm();"></iframe>
                                    </div>
				</td>
			</tr>
                    </table>
                </div>
                <div id="divSource" class="tmplDivOptionBox" style="display: none">
                    <b><span fcklang="wikiDlgTemplateSource">Template raw definition (from {{ to }})</span></b>
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
			<tr>
				<td class="tmplTable">
					<textarea id="xTemplateRaw" style="width: 100%; height: 100%; font-family: Monospace"
						cols="50" rows="10" wrap="off"></textarea>
				</td>
			</tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
</body>
</html>
